package com.xcd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcd.reggie.common.CustomException;
import com.xcd.reggie.entity.Category;
import com.xcd.reggie.entity.Dish;
import com.xcd.reggie.entity.Setmeal;
import com.xcd.reggie.mapper.CategoryMapper;
import com.xcd.reggie.service.CategoryService;
import com.xcd.reggie.service.DishService;
import com.xcd.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;



    @Override
    public void remove(Long ids) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId, ids);

        int count = dishService.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("该分类下关联了菜品，无法删除");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, ids);

        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("该分类下关联了套餐，无法删除");
        }

        super.removeById(ids);

    }
}